package cn.csl.basics.util.translate;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5
{
    private static final char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    public static String md5(String input)
    {
        if (input == null) {
            return null;
        }
        try
        {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");

            byte[] inputByteArray = input.getBytes("utf-8");

            messageDigest.update(inputByteArray);

            byte[] resultByteArray = messageDigest.digest();

            return byteArrayToHex(resultByteArray);
        }
        catch (NoSuchAlgorithmException e)
        {
            return null;
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        return input;
    }

    public static String md5(File file)
    {
        try
        {
            if (!file.isFile())
            {
                System.err.println("����" + file.getAbsolutePath() + "������������������");
                return null;
            }
            FileInputStream in = new FileInputStream(file);

            String result = md5(in);

            in.close();

            return result;
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static String md5(InputStream in)
    {
        try
        {
            MessageDigest messagedigest = MessageDigest.getInstance("MD5");

            byte[] buffer = new byte['?'];
            int read = 0;
            while ((read = in.read(buffer)) != -1) {
                messagedigest.update(buffer, 0, read);
            }
            in.close();

            return byteArrayToHex(messagedigest.digest());
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    private static String byteArrayToHex(byte[] byteArray)
    {
        char[] resultCharArray = new char[byteArray.length * 2];

        int index = 0;
        for (byte b : byteArray)
        {
            resultCharArray[(index++)] = hexDigits[(b >>> 4 & 0xF)];
            resultCharArray[(index++)] = hexDigits[(b & 0xF)];
        }
        return new String(resultCharArray);
    }
}
